﻿using System;
using System.IO;
using System.Data;
using System.Collections.Generic;

namespace NetFtp.Core.Ftp
{
    /// <summary>
    /// Represente un fichier sur un serveur FTP
    /// </summary>
    public class FtpFile
    {
        private string _FullName;
        private string _Name;
        private string _Extension;
        private long _Length;
        private string _LastAccessTime;
        private bool _Directory;
        private string _Droit;

        #region Constructeurs
        public FtpFile()
        {
            _Directory = false;
            _Length = 0;
            _FullName = "";
            _Extension = "";
            _LastAccessTime = "";
            _Name = "";
            _Droit = "";
        }

        public FtpFile(string FullName)
        {
            FileInfo tmpfile = new FileInfo(FullName);

            _FullName = FullName;
            _Name = tmpfile.Name;
            _Extension = tmpfile.Extension;
            if (_Extension == "")
                _Directory = true;
            _Length = 0;
            _Droit = "";
        }
        #endregion

        #region Proprietes
        /// <summary>
        /// Nom et chemin du fichier
        /// </summary>
        public string FullName
        {
            get
            {
                return _FullName;
            }
            set
            {
                _FullName = value;
            }
        }
        /// <summary>
        /// Nom du fichier
        /// </summary>
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
                FileInfo tmpfile = new FileInfo(_Name);
                _Extension = tmpfile.Extension;
            }
        }
        /// <summary>
        /// Extention du fichier
        /// </summary>
        public string Extension
        {
            get
            {
                return _Extension;
            }
            set
            {
                _Extension = value;
            }
        }
        /// <summary>
        /// Taille du fichier
        /// </summary>
        public long Length
        {
            get
            {
                return _Length;
            }
            set
            {
                _Length = value;
            }
        }
        /// <summary>
        /// Date de dernier accès au fichier
        /// </summary>
        public string LastAccessTime
        {
            get
            {
                return _LastAccessTime;
            }
            set
            {
                _LastAccessTime = value;
            }
        }
        /// <summary>
        /// Retourne vrai si dossier
        /// </summary>
        public bool Directory
        {
            get
            {
                return _Directory;
            }
            set
            {
                _Directory = value;
            }
        }
        /// <summary>
        /// Retourne les droits
        /// </summary>
        public string Droit
        {
            get
            {
                return _Droit;
            }
            set
            {
                _Droit = value;
            }
        }
        #endregion

        public override bool Equals(object obj)
        {
            bool retVal = false;
            if (obj != null)
            {
                FtpFile custObj = obj as FtpFile;
                if ((custObj.Directory == this.Directory) &&
                    (custObj.Extension.Equals(this.Extension) &&
                    (custObj.FullName.Equals(this.FullName)) &&
                    (custObj.LastAccessTime.Equals(this.LastAccessTime)) &&
                    (custObj.Length.Equals(this.Length)) &&
                    (custObj.Name.Equals(this.Name))))
                    retVal = true;
            }
            return retVal;
        }

        public class FtpFileComparer : IComparer<FtpFile>
        {
            #region IComparer<FtpFile> Members

            int IComparer<FtpFile>.Compare(FtpFile x, FtpFile y)
            {
                if (x.Equals(y))
                    return 0;
                // on trie par dossier
                int dirCriteria = ((x.Directory == y.Directory)
                    ? 0 : x.Directory ? -1 : 1);

                if (dirCriteria == 0)
                {
                    // puis par nom de fichier
                    int nameCriteria = x.Name.CompareTo(y.Name);

                    if (nameCriteria == 0)
                    {
                        // puis par extention
                        int extCriteria = x.Extension.CompareTo(y.Extension);

                        return extCriteria; // c'est tout
                    }
                    else
                    {
                        return nameCriteria;
                    }
                }
                else
                {
                    return dirCriteria;
                }
            }

            #endregion
        }
    }
}
